package cn.bisoft.java.service.ext.httpclient;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hengpeng.common.configuration.ConfigProvider;
import com.hengpeng.common.configuration.ConfigProviderFactory;
import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.constant.SignatureType;
import com.hengpeng.common.exception.MessageFormatException;
import com.hengpeng.common.msg.agentportal.MessageConstant;
import com.hengpeng.common.msg.agentportal.MessageConverterImpl;
import com.hengpeng.common.msg.agentportal.MessageHelper;
import com.hengpeng.common.msg.agentportal.TransMessage;
import com.hengpeng.common.msg.agentportal.TransType;

public class LotteryNotifyFacade
{
	private static final Log logger = LogFactory.getLog(LotteryNotifyFacade.class);

	private String localPrivateKey;

	private String remotePublicKey;

	private String signType;

	public LotteryNotifyFacade()
	{
		ConfigProvider provider = ConfigProviderFactory.getInstance(Constant.HP_COMMON_PROPERTIES_FILEPATH);
		this.localPrivateKey = (String) provider.getConfigByPrimaryKey(Constant.AGENTPORTAL_PRIVATEKEY);
		this.remotePublicKey = (String) provider.getConfigByPrimaryKey(Constant.AGENTPORTAL_PUBLICKKEY);
		this.signType = (String) provider.getConfigByPrimaryKey(Constant.AGENTPORTAL_SIGNTYPE);
	}

	public LotteryNotifyFacade(String signType, String localPrivateKey, String remotePublicKey)
	{
		this.localPrivateKey = localPrivateKey;
		this.remotePublicKey = remotePublicKey;
		this.signType = signType;
	}

	public String process(String xml, String transType, final NotifyCallback callback)
	{
		TransType valueOf = TransType.valueOf(transType);
		TransMessage rtnMessage = null;
		try
		{
			final TransMessage message = MessageConverterImpl.getInstance().deserialize(xml, valueOf.value(),
					this.remotePublicKey, this.signType);
			new Thread(new Runnable()
			{
				public void run()
				{
					callback.callback(message);
				}
			}).start();
			rtnMessage = MessageHelper.createResponseMessage(message, valueOf.obverse());
		}
		catch (MessageFormatException e)
		{
			rtnMessage = MessageHelper.createResponseMessage(xml, valueOf.obverse(), e.getExceptionCode());
		}
		return this.createReturnContent(valueOf.obverse(), rtnMessage);
	}

	private String createReturnContent(int transType, TransMessage message)
	{
		StringBuffer rtnXml = new StringBuffer();
		rtnXml.append(MessageConstant.MESSAGE_AGENTPORTAL_NAMING_TRANSTYPE);
		rtnXml.append("=");
		rtnXml.append(transType);
		rtnXml.append("&");
		rtnXml.append(MessageConstant.MESSAGE_AGENTPORTAL_NAMING_TRANSMESSAGE);
		rtnXml.append("=");
		try
		{
			if (message.getHeader().getMessengerID() != "-1")
			{
				if (SignatureType.RSA.name().equalsIgnoreCase(this.signType))
				{
					rtnXml.append(MessageConverterImpl.getInstance().serialize(message, transType,
							this.localPrivateKey, SignatureType.RSA.name()));
				}
				else
				{
					rtnXml.append(MessageConverterImpl.getInstance().serialize(message, transType,
							this.localPrivateKey, null));
				}
			}
			else
			{
				rtnXml.append(MessageConverterImpl.getInstance().serialize(message, transType, "", ""));
			}
			return rtnXml.toString();
		}
		catch (MessageFormatException e)
		{
			logger.error("��Ϣ��ת��Ϊ�ַ�ʱ�쳣", e);
			return "";
		}
	}
}
